package com.lemote.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @author wuhui
 * @time 2011-7-17 下午08:02:16
 * @Version:0.0.1
 * @since smt-utils
 */
public class StringUtils {

	/**
	 * 字符串转换成char数组
	 * 
	 * @author wuhui
	 * @time 2011-7-17 下午07:58:47
	 * @Version:0.0.1
	 * @param str
	 * @return
	 */

	public static char[] StringToArray(String str) {
		if (str == null) {
			return null;
		}
		char[] arr = new char[str.length()];
		for (int i = 0; i < str.length(); i++) {
			arr[i] = str.charAt(i);
		}
		return arr;
	}
	
	/**
	 * 获取两个字符串相同的部分
	  * @author wuhui
	  * @time 2011-7-17 下午08:06:56
	  * @Version:0.0.1
	  * @param str1
	  * @param str2
	  * @return
	 */
	public static String getSameString(String str1,String str2){
		char[] arr = StringToArray(str1); 
		StringBuffer sb =  new StringBuffer();
		for (int i=0;i<arr.length;i++) {
			sb.append(arr[i]);
			if (!str2.startsWith(sb.toString())){
				sb.deleteCharAt(i);
				return sb.toString();
			}
		}
		return sb.toString();
	}
	/**
	  * @param date
	  * @return
	  */
	public static String dateToString(Date date) {
		if (date==null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	
}
